//
//  LXHomeButtonViewCell.m
//  LvXiShopping
//
//  Created by 诸葛孔明 on 16/5/27.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXHomeButtonViewCell.h"

#import "LXHomeButton.h"

#import "LXWebViewController.h"

#import "SignViewController.h"

#import "LXMallShareController.h"

#import "CategoryViewController.h"



@interface LXHomeButtonViewCell()

@property(nonatomic,strong)NSArray *imageArray;
@property(nonatomic,strong)NSArray *titles;

@end

@implementation LXHomeButtonViewCell
- (NSArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = @[@"home_but_green",@"home_but_wine",@"but_sign_n"];
//        _imageArray = @[];
    }
    return _imageArray;
}

- (NSArray *)titles
{

    if (!_titles) {
        _titles = @[@"营养保健",@"酒水茶饮",@"每日签到"];
//        _titles = @[];
    }
    return _titles;
}

-(void)setCates:(NSArray<NSDictionary *> *)cates{
    _cates = cates;
    
    for (id obj in self.subviews) {
        [obj removeFromSuperview];
    }
    
    if (!_cates) {
        return;
    }
    CGFloat X = 10;
    CGFloat Y = 0;
    CGFloat space = 35;
    CGFloat space_h = 10;
    CGFloat W = ([UIScreen mainScreen].bounds.size.width - space*5)/ 4;
    CGFloat H = W+30;
    for (int i = 0 ; i<_cates.count; i++) {
        NSDictionary * buttonItem = _cates[i];
       __block LXHomeButton *button = [[LXHomeButton alloc] init];
        X = i%4 *(W + space)+space;
        Y = i/4*(H + space_h);
        button.frame = CGRectMake(X, Y, W, H);
        
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [[NetRequest request] downloadImageWithUrl:buttonItem[@"thumbnail_pic"] requestHandle:^(BOOL success, id responseDict) {
            if(success){
                [button setImage:responseDict forState:UIControlStateNormal];
            }
        }];

        [button setTitle:buttonItem[@"cat_name"] forState:UIControlStateNormal];
        [self addSubview:button];
        
    }
    self.cellHeight = ceil(self.cates.count/4.0)*(H+space_h);
}




- (void)awakeFromNib {
    [super awakeFromNib];
    
    NSInteger count = self.imageArray.count;
    
    CGFloat X = 10;
    CGFloat Y = 0;
    CGFloat W = ([UIScreen mainScreen].bounds.size.width - 120)/ count;
    CGFloat H = W+20;
    
    for (int i = 0; i < count; i ++) {
        LXHomeButton *button = [[LXHomeButton alloc] init];
        X = i * W + 10;
        button.frame = CGRectMake(X, Y, W, H);
        
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        [button setTitle:self.titles[i] forState:UIControlStateNormal];
        [self addSubview:button];
    }
    self.cellHeight = H;
}




- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        NSInteger count = self.imageArray.count;
        CGFloat space = 40;
        CGFloat X = 0;
        CGFloat Y = 0;
        CGFloat W = ([UIScreen mainScreen].bounds.size.width - space*(count+1))/ count;
        CGFloat H = W+30;
        for (int i = 0; i < count; i ++) {
            LXHomeButton *button = [[LXHomeButton alloc] init];
            X = i * (W+space) + space;
            button.frame = CGRectMake(X, Y, W, H);
            button.tag = i;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [button setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
            [button setTitle:self.titles[i] forState:UIControlStateNormal];
            
            [self addSubview:button];
        }
        
        self.cellHeight = H;
    }
    return self;
}

- (void)buttonClick:(LXHomeButton *)button
{
    NSDictionary * buttonItem = self.cates[button.tag];
    if(self.handleEnevts){
        self.handleEnevts(buttonItem[@"cat_id"]);
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    //[super setSelected:selected animated:animated];

    
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    
}

@end
